/**
 * Created by Administrator on 2016/11/11.
 */
(function(){
    var login={
        init:function(){
            this.tel=$(".tel");
            this.validate();
            this.sendsms();
            this.submit();
        },
        validate:function(){
            var _this=this;
            this.tel.on("blur",function(){
                var telVal=_this.tel.val();

                if(!/^1\d{10}$/ig.test(telVal)){
                    _this.tel.addClass("error");
                    layer.msg("手机号输入有误", {icon:2,time:1000});
                    $(".tel").val("");
                }else{
                    $.post(get_loginUrl()+"Cregister/check_register_user",{mobile:telVal},function(json){
                        if(json>0){
                            _this.tel.addClass("error");
                            layer.msg("该号码已注册", {icon:2,time:1000});
                            $(".tel").val("");
                        }
                    });
                }


            });
            this.tel.on("focus",function(){
                _this.tel.removeClass("error");
            });
        },
        sendsms:function(){
        	var _this = this;
			var timestamp = $("#register_form").find("input[name=timestamp]").val();
			if(timestamp){
				$("#register_form").on("click", ".sendsms", function(){
					var this_a = $("#register_form").find('.s_code');
					var num = 60;
					var t;
					var second = function(){
						this_a.html(num +'秒后可重发');
						t = setTimeout(second,1000);
						if(num === 0){
							clearTimeout(t);this_a.addClass('sendsms');this_a.html('获取验证码');
						}
						num --;
					}
					if(_this.tel.val()){
						if(!_this.tel.hasClass("error")){
							this_a.removeClass('sendsms');
							$.post(get_loginUrl()+'Cregister/send_sms', {mobile:_this.tel.val(),timestamp:timestamp}, function(json){
								if(json == false) {
									this_a.addClass('sendsms');
									layer.msg('短信发送失败',{icon:2,time:1000});
									//location.reload();
								}else{
									second();
									layer.msg('短信发送成功',{icon:1,time:1000});
									setTimeout(function(){this_a.addClass('sendsms');},60*1000);
								}
							},'json');
						}else{
							layer.msg('手机号错误',{icon:2,time:1000});
						}
					}else{
						layer.msg('请填写手机号',{icon:2,time:1000});
					}
				});
			}else{
				layer.msg('无效访问，请重新访问',{icon:2,time:3000});
				location.reload();
			}
        },
        submit:function(){
        	var _this = this;
        	if ($("#register_form").size() > 0){
        		$('form#register_form').submit(function(e) {
        			
        			if(!/^1\d{10}$/ig.test($(".tel").val())){
        				$(".tel").addClass("error");
                        layer.msg("手机号输入有误", {icon:2,time:1000});
                        $(".tel").val("");
                        return false;
                    }
                    if(!$(".code").val()){
                    	$(".code").addClass("error");
                        layer.msg("请输入验证码", {icon:2,time:1000});
                        return false;
                    }
                    $("#register_form input.error").removeClass("error");

        		});
        	}
        }
    };
    login.init();
})();